package cc.vicp.djx314.starfood.web.validator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import cc.vicp.djx314.starfood.domain.account.User;
import cc.vicp.djx314.starfood.service.account.AccountService;
import cc.vicp.djx314.starfood.service.account.UserInfoService;

@Component
public class UserUpdateValidator implements Validator {
	@Autowired
	UserInfoService userInfoService;

	@Autowired
	AccountService accountService;

	@Override
	public boolean supports(Class<?> arg0) {
		return User.class.isAssignableFrom(arg0);
	}

	@Override
	public void validate(Object target, Errors errors) {
		User user = (User) target;
		if (userInfoService.getIfOtherUserNickNameExists(user)) {
			errors.rejectValue("nickName", null, "昵称已经存在");
		}
	}
}